package com.qianguo.shop.fragment;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.shop.R;
import com.qianguo.shop.base.BaseFragment;
import com.eva.androidlibrary.api.entity.OrderInfo;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;
import com.qianguo.shop.entity.OrderList;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * @author GUXIA
 * @since 2015-10-11
 */
@EFragment(R.layout.fragment_doing_order)
public class DoingOrderFragment extends BaseFragment {

    @ViewById(R.id.swipe_refresh_recycler_view)
    SwipeRefreshRecyclerView mSwipeRefreshRecyclerView;

    private List<OrderInfo> mOrders = new ArrayList<>();
    private int mCurrentPage = 1;
    private int mTotal;
    private static final int PER_PAGE = 10;

    private DoingOrderAdapter mAdapter = null;

    @Override
    protected void initializeUI() {
        final Context ctx = getApplicationContext();
        mAdapter = new DoingOrderAdapter(ctx);
        mSwipeRefreshRecyclerView.setAdapter(mAdapter);
        mSwipeRefreshRecyclerView.setSwipeRefreshListener(new SwipeRefreshRecyclerView.SwipeRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mOrders.clear();
                loadData(mCurrentPage, PER_PAGE);
            }

            @Override
            public void onLoadMore() {
                loadData(++mCurrentPage, PER_PAGE);
            }
        });

        loadData(mCurrentPage, PER_PAGE);
    }

    @Background
    void loadData(int page, int perPage) {
        mApp.getApi().getOrders(mApp.getUser().getToken(), "", OrderInfo.STATUS_DOING, page, perPage, new HttpRequestCallback<OrderList>() {
            @Override
            public void onSuccess(int code, String msg, OrderList data) {
                mTotal = data.getTotal();
                mOrders.addAll(data.getList());
                refreshUI();
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    @UiThread
    void refreshUI() {
        mSwipeRefreshRecyclerView.onCompleted();
        mSwipeRefreshRecyclerView.notifyDataSetChanged();
    }

    private class DoingOrderAdapter extends RecyclerView.Adapter<DoingOrderViewHolder> {

        private Context mContext;
        private DoingOrderAdapter(Context context) {
            mContext = context;
        }

        @Override
        public DoingOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_processing_order, parent, false);
            return new DoingOrderViewHolder(v);
        }

        @Override
        public void onBindViewHolder(DoingOrderViewHolder holder, int position) {
            holder.onBind(mOrders.get(position));
        }

        @Override
        public int getItemCount() {
            return mOrders != null ? mOrders.size() : 0;
        }
    }

    private class DoingOrderViewHolder extends RecyclerView.ViewHolder {

        TextView txvUsername;
        TextView txvAddress;
        TextView txvHandleTime;
        TextView txvOrderTime;
        TextView txvTotalAmount;
        Button btnCheck;

        private OrderInfo item;

        public DoingOrderViewHolder(final View itemView) {
            super(itemView);
            txvUsername = (TextView) itemView.findViewById(R.id.processing_order_username);
            txvAddress = (TextView) itemView.findViewById(R.id.processing_order_addr_value);
            txvHandleTime = (TextView) itemView.findViewById(R.id.processing_order_date_handle_value);
            txvOrderTime = (TextView) itemView.findViewById(R.id.processing_order_date_added_value);
            txvTotalAmount = (TextView) itemView.findViewById(R.id.processing_order_amount);
            btnCheck = (Button) itemView.findViewById(R.id.processing_order_btn_check);
            btnCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    if (item instanceof MockBaseOrderInfo) {
//                        switch (item.getDoingProgress()) {
//                            case OrderInfo.DOING_PROGRESS_WEIGHT:
//                                //TODO 去称重
//                                OrderDetailsActivity.mockJumpToInitOrderDetails(getActivity(),
//                                        OrderDetailsActivity.TYPE_WEIGHING, (MockBaseOrderInfo) item);
//                                break;
//                            case OrderInfo.DOING_PROGRESS_PUBLISH:
//                                //TODO 发布配送
//                                break;
//                            case OrderInfo.DOING_PROGRESS_RUSH:
//                                //TODO 催配送
//                                break;
//                        }
//                    }
                }
            });
        }

        void onBind(OrderInfo info) {
            txvUsername.setText(info.getUserName());
            txvAddress.setText(info.getReceiverAddress());
            txvHandleTime.setText(info.getDeliveryTime());
//            txvOrderTime.setText(info.getOrderTime());
            txvTotalAmount.setText(String.valueOf(info.getPrice()));
            item = info;
        }
    }
}
